﻿# Makefile for C language exercise project

# Define the directory where the exercise files are stored
EXERCISE_DIR = ./exercises

# Define the test directory
TEST_DIR = ./test

# Define the build directory
BUILD_DIR = ./build

# Define the input and expected output directories
INPUT_DIR = ./input
EXPECTED_DIR = ./expected

# Ensure the test, build, input, and expected directories exist
$(shell mkdir -p $(TEST_DIR) $(BUILD_DIR) $(INPUT_DIR) $(EXPECTED_DIR))

# Define the list of exercises
EXERCISES = $(wildcard $(EXERCISE_DIR)/*.c)

# Define the list of executables
EXECUTABLES = $(patsubst $(EXERCISE_DIR)/%.c, $(BUILD_DIR)/%, $(EXERCISES))

# Define compiler and linker flags
CC = gcc
CFLAGS = -Wall -Wextra -std=c99
LDFLAGS = -lm

# Default target: build all executables
all: $(EXECUTABLES)

# Build rule for each executable
$(BUILD_DIR)/%: $(EXERCISE_DIR)/%.c
	$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS)

# Clean rule to remove only executables and .d.out files
clean:
	find $(BUILD_DIR) -type f \( -name "*.d.out" -o -name "*" ! -name "*.json" \) -exec rm -f {} +

# Check if colorama is installed, if not, install it
check-colorama:
	@python3 -c "import colorama" 2>/dev/null || pip install colorama

# Run Python script to test executables
test-output: check-colorama $(EXECUTABLES)
	python3 test_runner.py

# New target to save test results and count pass rate in JSON format
save-test-results: check-colorama $(EXECUTABLES)
	python3 test_runner.py

.PHONY: all clean test-output save-test-results check-colorama
